The beam coupling impedances of small discontinuities of an accelerator vacuum chamber have been calculated [e.g., S.S. Kurennoy, R.L. Gluckstern, and G.V. Stupakov, Phys. Rev. E 52, 4354 (1995)] for ultrarelativistic beams using the Bethe diffraction theory. Here we extend the results to an arbitrary beam velocity. The vacuum chamber is assumed to have an arbitrary, but uniform along the beam path, cross section. The longitudinal and transverse coupling impedances are derived in terms of series over cross-section eigenfunctions, while the discontinuity shape enters via its polarizabilities. Simple explicit formulas for two important particular cases - circular and rectangular chamber cross sections - are presented. The impedance dependence on the beam velocity exhibits some unusual features: for example, the reactive impedance, which dominates in the ultrarelativistic limit, can vanish at a certain beam velocity, or its magnitude can exceed the ultrarelativistic value many times. In addition, we demonstrate that the same technique, the field expansion into a series of cross-section eigenfunctions, is convenient for calculating the space-charge impedance of uniform beam pipes with arbitrary cross section.